package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.result.ResultInfo;
import com.woniuxy.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2021/11/24 11:51
 * @FileName: UserController
 * @version: 1.0
 * @Description: 用户控制器 测试基本的CRUD
 */
@Log4j2
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 注入service对象
     */
    @Resource
    private UserService userService;

    /**
     * 新增对象
     * @param user  用户对象
     * @return  响应结果集
     */
    @PostMapping("/u")
    public ResultInfo<?> addUser(@RequestBody User user){
        log.info(user);
      int row =  userService.addUser(user);

      if (row > 0) {
          return new ResultInfo<>(200,"插入成功",row);
      }else {
          return new ResultInfo<>(400,"插入成功",row);
      }
    }
    /**
     * 修改对象
     * @param user  用户对象
     * @return  响应结果集
     */
    @PutMapping("/u")
    public ResultInfo<?> updateUser(@RequestBody User user){
        log.info(user);
        int row =  userService.updateUser(user);

        if (row > 0) {
            return new ResultInfo<>(200,"修改成功",row);
        }else {
            return new ResultInfo<>(400,"修改失败",row);
        }
    }
    /**
     * 查询所有对象
     * @param user  用户对象,不作为查询条件
     * @return  响应结果集
     */
    @GetMapping("/u")
    public ResultInfo<List<User>> findUser(@RequestBody User user){
        log.info(user);
        List<User> users =  userService.findUser(user);

        if (users != null) {
            return new ResultInfo<>(200,"修改成功",users);
        }else {
            return new ResultInfo<>(400,"修改失败",null);
        }
    }
    /**
     * 删除对象
     * @param user  用户对象
     * @return  响应结果集
     */
    @DeleteMapping("/u")
    public ResultInfo<?> deleteUser(@RequestBody User user){
        log.info(user);
        int row =  userService.deleteUser(user);
        if (row > 0) {
            return new ResultInfo<>(200,"删除成功",row);
        }else {
            return new ResultInfo<>(400,"删除失败",row);
        }
    }


}
